package com.helmlabs.grouper.accounts.service;

import java.util.Iterator;
import java.util.List;

import javax.jws.WebService;

import org.mortbay.log.Log;

import com.helmlabs.grouper.accounts.AccountApplicationContext;
import com.helmlabs.grouper.accounts.dao.CompanyDao;
import com.helmlabs.grouper.accounts.dao.RoleDao;
import com.helmlabs.grouper.accounts.dao.UserProfileDao;
import com.helmlabs.grouper.accounts.model.Company;
import com.helmlabs.grouper.accounts.model.Role;
import com.helmlabs.grouper.accounts.model.UserProfile;
import com.helmlabs.grouper.core.util.SecurityUtil;

@WebService
public class AccountManagerImpl implements AccountManager{
	
	private UserProfileDao userProfileDao = (UserProfileDao)AccountApplicationContext.getInstance().getBean("userProfileDao");
	private RoleDao roleDao = (RoleDao)AccountApplicationContext.getInstance().getBean("roleDao");
	private CompanyDao companyDao = (CompanyDao)AccountApplicationContext.getInstance().getBean("companyDao");
	
	public UserProfile getUserProfile(Long id){
		return userProfileDao.get(id);
	}
	
	public UserProfile getUserProfileByUsername(String username){
		return userProfileDao.getByUsername(username);
	}
	
	
	public Company getCompanyById(Long id) {
		return companyDao.get(id);
	}

	public List<UserProfile> getUserProfiles(int page, int pageSize,
			String sort, String order) {
		return userProfileDao.getAll(page, pageSize, sort, order);
	}

	public UserProfile saveUserProfile(UserProfile userProfile) {
		return userProfileDao.save(userProfile);
	}
	
	public UserProfile getCurrentUser(){
		return userProfileDao.getByUsername(SecurityUtil.getCurrentUsername());
	}

	public boolean isUserMemberOfRole(Long userid, Long roleid, boolean recursive) {
		if (recursive){
			return roleDao.isObjectInRoleRecursive(Role.USER_ROLES, userid, roleid);
		} else {
			return roleDao.isObjectInRole(Role.USER_ROLES, userid, roleid);
		}
	}
	
	public boolean isCompanyMemberOfRole(Long companyid, Long roleid, boolean recursive) {
		if (recursive){
			return roleDao.isObjectInRoleRecursive(Role.COMPANY_ROLES, companyid, roleid);
		} else {
			return roleDao.isObjectInRole(Role.COMPANY_ROLES, companyid, roleid);
		}
	}
	
		
	public boolean activateUserInRole(Long userid, Long roleid){
		return roleDao.activateObjectInRole(Role.USER_ROLES, userid, roleid);
	}
	
	public boolean deactivateUserOutOfRole(Long userid, Long roleid){
		return roleDao.deactivateObjectInRole(Role.USER_ROLES, userid, roleid);
	}

	public List<Role> getCompanyRolesByParent(Long parentid) {
		return roleDao.getAllRolesByTypeAndParent(Role.COMPANY_ROLES, parentid);
	}

	public List<Role> getUserRolesByParent(Long parentid) {
		return roleDao.getAllRolesByTypeAndParent(Role.USER_ROLES, parentid);
	}

	public boolean isUserMemberOfAnyRoles(Long userid, List<Long> roleids,
			boolean recursive) {
		System.out.println("testing to see if this user is a member of any of the roles " + roleids);
		for (Long roleid : roleids){
			if (isUserMemberOfRole(userid, roleid, recursive)){
				return true;
			}
		}
		return false;
	}
	
	public boolean isUserMemberOfAllRoles(Long userid, List<Long> roleids,
			boolean recursive) {
		for (Long roleid : roleids){
			if (!isUserMemberOfRole(userid, roleid, recursive)){
				return false;
			}
		}
		return true;
	}

	public Company getCurrentCompanyByUserId(Long id) {
		return companyDao.getCurrentCompanyByUserId(id);
	}

	public List<Company> getCompanies(int page, int pageSize, String sort,
			String direction) {
		return companyDao.getPage(page, pageSize, sort, direction);
	}

	public List<UserProfile> getEmployeesByCompanyId(Long companyid) {
		// TODO Auto-generated method stub
		return userProfileDao.getAllEmployeesByCompanyId(companyid);
	}
	
}
